
import { createWebHistory, createRouter } from 'vue-router'
import {  wmsStore  } from '@/store/pinia'
import { ElMessage } from 'element-plus'



const routers = [
    {
        path: '/',
        name: 'home',
        meta: { title: '首页' ,icon: "HomeFilled" },
        component: () => import('@/views/Home.vue')
    },
    {
        path: '/user2',
        name: 'user2',
        meta: { title: '个人中心2' ,icon: "HomeFilled",isShow: false},
        component: () => import('@/views/User.vue')
    },
    {
        path: '/product',
        name: 'product',
        meta: { title: '商品分类',icon:"Menu",/* roles: ['ROLE_PRODUCT'] */},
        children: [
            {
                path: '/product/list',
                name: 'product-list',
                meta: { title: '商品列表' ,icon:"Tickets"},
                component: () => import('@/views/product/List.vue')
            },
            {
                path: '/product/edit',
                name: 'product-edit',
                meta: { title: '编辑商品' ,icon:"Tickets",isShow: false},
                component: () => import('@/views/product/Edit.vue')
            },
            {
                path: '/product/add',
                name: 'product-add',
                meta: { title: '添加商品' ,icon:"Plus"},
                component: () => import('@/views//product/Add.vue')
            }
        ]
    },
    {
        path: '/category',
        name: 'category',
        meta: { title: '分类中心' ,icon:"Menu",/*roles: ['ROLE_PRODUCT']*/},
        children: [
            {
                path: '/category/list',
                name: 'category-list',
                meta: { title: '分类列表' ,icon:"Tickets"},
                component: () => import('@/views/category/List.vue')
            },
            {
                path: '/category/add',
                name: 'category-add',
                meta: { title: '添加分类',icon:"Plus" ,isShow: false},
                component: () => import('@/views/category/Add.vue')
            },
            {
                path: '/category/edit',
                name: 'category-edit',
                meta: { title: '编辑分类',icon:"Plus" ,isShow: false},
                component: () => import('@/views/category/Edit.vue')
            }
        ]
    },
    {
        path: '/warehouse',
        name: 'warehouse',
        meta: { title: '仓库管理',icon:"Menu",/*roles:['PO_STORE','PO_STORE_MANAGER']*/ },
        children: [
            {
                path: '/warehouse/list',
                name: 'warehouse-list',
                meta: { title: '货架列表' ,icon:"Tickets"},
                component: () => import('@/views/warehouse/List.vue')
            },
            {
                path: '/warehouse/manager',
                name: 'warehouse-manager',
                meta: { title: '库房管理' ,icon:"MessageBox"},
                component: () => import('@/views/warehouse/Manager.vue')
            }
        ]
    },
    {
        path: '/vendor',
        name: 'vendor',
        meta: { title: '供应商管理',icon:"Grid",/* roles:['PO_PURCHASE','PO_PURCHASE_MANAGER'] */},
        children: [
            {
                path: '/vendor/list',
                name: 'vendor-list',
                meta: { title: '供应商列表' ,icon:"Tickets"},
                component: () => import('@/views/vendor/List.vue')
            },
            {
                path: '/vendor/edit',
                name: 'vendor-edit',
                meta: { title: '编辑供应商' ,icon:"Tickets",isShow: false},
                component: () => import('@/views/vendor/Edit.vue')
            },
            {
                path: '/vendor/add',
                name: 'vendor-add',
                meta: { title: '添加供应商' ,icon:"Plus"},
                component: () => import('@/views/vendor/Add.vue')
            },
            {
                path: '/vendor/map',
                name: 'vendor-map',
                meta: { title: '供应商地图' ,icon:"Plus"},
                component: () => import('@/views/vendor/Map.vue')
            }
        ]
    },
    {
        path: '/po',
        name: 'po',
        meta: { title: '采购单管理',icon:"Menu",/* roles:['ROLE_PURCHASE','ROLE_PURCHASE_MANAGER'] */ },
        children: [
            {
                path: '/po/list',
                name: 'po-list',
                meta: { title: '采购单列表' ,icon:"Tickets"},
                component: () => import('@/views/po/List.vue')
            },
            {
                path: '/po/add',
                name: 'po-add',
                meta: { title: '新建采购单' ,icon:"Plus"},
                component: () => import('@/views/po/Add.vue')
            },
            {
                path: '/po/detail',
                name: 'po-detail',
                meta: { title: '采购单详情' ,icon:"Plus",isShow: false},
                component: () => import('@/views/po/Detail.vue')
            }
        ]
    },
    
    {
        path: '/deptment',
        name: 'deptment',
        meta: { title: '部门管理' ,icon: "HomeFilled"},
        children: [
            {
                path: '/dept/list',
                name: 'dept-list',
                meta: { title: '部门列表' ,icon:"UserFilled" },
                component: () => import("@/views/deptment/List.vue")
            },
            {
                path: '/dept/add',
                name: 'dept-add',
                meta: { title: '添加部门' ,icon:"UserFilled" },
                component: () => import("@/views/deptment/Add.vue")
            },
            
        ]
    },
    {
        path: '/employee',
        name: 'employee',
        meta: { title: '员工管理' ,icon: "HomeFilled"},
        children: [
            {
                path: '/empl/list',
                name: 'empl-list',
                meta: { title: '员工列表' ,icon:"UserFilled" },
                component: () => import("@/views/employee/List.vue")
            },
            {
                path: '/empl/add',
                name: 'empl-add',
                meta: { title: '添加员工' ,icon:"UserFilled" },
                component: () => import("@/views/employee/Add.vue")
            },
            {
                path: '/empl/edit',
                name: 'empl-edit',
                meta: { title: '员工信息编辑' ,icon:"UserFilled" },
                component: () => import("@/views/employee/Edit.vue")
            },
            
        ]
    },
    {
        path: '/roles',
        name: 'roles',
        meta: { title: '角色管理' ,icon: "HomeFilled"},
        children: [
            {
                path: '/role/list',
                name: 'role-list',
                meta: { title: '角色列表' ,icon:"UserFilled" },
                component: () => import("@/views/roles/List.vue")
            },
            {
                path: '/role/add',
                name: 'role-add',
                meta: { title: '添加角色' ,icon:"UserFilled" },
                component: () => import("@/views/roles/Add.vue")
            },
            {
                path: '/role/edit',
                name: 'role-edit',
                meta: { title: '角色信息编辑' ,icon:"UserFilled" },
                component: () => import("@/views/roles/Edit.vue")
            },
            
        ]
    },
    {
        path: '/user',
        name: 'user',
        meta: { title: '用户中心', icon: "HomeFilled", isUseFrame: false, isShow: false },
        children: [
            {
                path: '/user/login',
                name: 'user-login',
                meta: { title: '用户登录', icon: "MessageBox", isUseFrame: false, isShow: false },
                component: () => import('@/views/user/Login.vue')
            },
            {
                path: '/user/register',
                name: 'user-register',
                meta: { title: '用户注册', icon: "MessageBox", isUseFrame: false, isShow: false },
                component: () => import('@/views/user/Register.vue')
            },

        ]
    },


]

const router = createRouter(
    {
        history: createWebHistory(),
        routes: routers
    }
)


router.beforeEach((to, from, next) => { 
    const wms_store = wmsStore()
    if(to.name == 'user-login' || to.name == 'user-register') {
        next()
    }
    if(wms_store.token == ''){
        next("/user/login")
    }else {
        if (to.meta.roles != undefined) {
            let rolesArray = to.meta.roles
            let b = wms_store.roles.some((item: any) => rolesArray.includes(item))
            if (b) {
                next()
            } else {
                ElMessage.error("无权限访问")
                next("/user/login")
            }
        }
    }
    next()
})


export default router